﻿Ext.define('ycsoft.list.oa.panels.oacarmanagelistpanel', {
    extend: 'Ext.panel.Panel',
    requires: [
        'YZSoft.BPM.src.ux.FormManager',
        'YZSoft.src.ux.RecordSecurityManager',
        'ycsoft.list.oa.models.oacarmanagelistmodel'
    ],
    dlgCfg: {
        dlgModel: 'Tab', //Tab,Window,Dialog
        width: 700,
        height: 650
    },
    constructor: function (config) {
        var me = this;

        //alert(Ext.encode(config.perm));       
        this.store = Ext.create('Ext.data.JsonStore', {
            remoteSort: true,
            pageSize: YZSoft.EnvSetting.PageSize.defaultSize,
            model:'ycsoft.list.oa.models.oacarmanagelistmodel',
            sorters: { property: 'id', direction: 'DESC' },
            proxy: {
                type: 'ajax',
                url: YZSoft.$url("ycsoft/list/oa/services/oacarmanagelistservices.ashx"),
                extraParams: {
                    method: 'GetData'
                },
                reader: {
                    rootProperty: 'children'
                }
            },
            listeners: {
                beforeload: function () {
                },
                load: function () {
                }
            }
        });

        this.grid = Ext.create('Ext.grid.Panel', {
            store: this.store,
            border: false,
            selModel: { mode: 'MULTI' },

            columns: {
                items: [
                    { xtype: 'rownumberer' },
                    { header: '主键', dataIndex: 'id', width: 100, align: 'right', sortable: true, hidden:true },
                   // { header: 'TaskID', dataIndex: 'TaskID', width: 100, align: 'right', sortable: true, hidden:true },
                    { header: '车辆编号', dataIndex: 'car_code', width: 120, align: 'left', sortable: true, hidden:false },
                   // { header: '使用责任人编码', dataIndex: 'car_user_code', width: 100, align: 'left', sortable: true, hidden:true },
                    { header: '使用责任人', dataIndex: 'car_user_name', width: 100, align: 'left', sortable: true, hidden:false },
                   // { header: '部门编码', dataIndex: 'app_dept_code', width: 100, align: 'left', sortable: true, hidden:true },
                  //  { header: '部门', dataIndex: 'app_dept_name', width: 100, align: 'left', sortable: true, hidden:true },
                    //{ header: '公司编码', dataIndex: 'app_company_code', width: 100, align: 'left', sortable: true, hidden:true },
                   // { header: '公司', dataIndex: 'app_company_name', width: 100, align: 'left', sortable: true, hidden:true },
                    { header: '车牌号', dataIndex: 'plate_number', width: 150, align: 'left', sortable: true, hidden:false },
                   // { header: '车型编号', dataIndex: 'car_type_code', width: 100, align: 'left', sortable: true, hidden:true },
                    { header: '车型名称', dataIndex: 'car_type_name', width: 150, align: 'left', sortable: true, hidden:false },
                    { header: '车辆购买日期', dataIndex: 'car_buy_date', width: 100, align: 'right', sortable: true, hidden:false },
                    { header: '备注', dataIndex: 'remarks', width: 300, align: 'left', sortable: true, hidden:false },

                    { header: '', dataIndex: 'tempcol', flex: 1, width: 100, align: 'left', sortable: true }
                ]
            },
            autoExpandColumn: 'extcol', 
            bbar: Ext.create('Ext.toolbar.Paging', {
                store: this.store,
                displayInfo: true
            }),
            listeners: {
                scope: this,
                rowdblclick: function (grid, record, tr, rowIndex, e, eOpts) {
                    me.read(record);
                }
            }
        });

        // 新增(表单服务)
        this.btnNewApp = new Ext.button.Button({
            iconCls: 'yz-glyph yz-glyph-new',
            text: '增加',
            scope: this,
            handler: function () {
                me.addNewApp();
            }
        });


        // 新增(流程)
        this.btnNewProc = new Ext.button.Button({
            iconCls: 'yz-glyph yz-glyph-new',
            text: '增加',
            scope: this,
            handler: function (item) {
                me.addNewProc();
            }
        });

        this.btnEditApp = Ext.create('YZSoft.src.button.Button', {
            // 编辑(表单服务)
            iconCls: 'yz-glyph yz-glyph-edit',
            text: '编辑',
            handler: function () {
                var sm = me.grid.getSelectionModel(),
                    recs = sm.getSelection() || [];

                if (recs.length != 1)
                    return;

                me.editApp(recs[0]);

            }
        });

        this.btnEditProc = Ext.create('YZSoft.src.button.Button', {
            // 编辑(流程)
            iconCls: 'yz-glyph yz-glyph-edit',
            text: '编辑',
            handler: function () {
                var sm = me.grid.getSelectionModel(),
                    recs = sm.getSelection() || [];

                if (recs.length != 1)
                    return;

                me.editProc(recs[0]);

            }
        });

        this.btnDeleteProc = Ext.create('YZSoft.src.button.Button', {
            // 删除(流程)
            iconCls: 'yz-glyph yz-glyph-delete',
            text: '删除',
            handler: function () {
                var sm = me.grid.getSelectionModel(),
                    recs = sm.getSelection() || [];

                if (recs.length != 1)
                    return;
                me.deleteProc(recs);
            }
        });
        this.btnDelete = Ext.create('YZSoft.src.button.Button', {
            // 删除(直接删除)
            iconCls: 'yz-glyph yz-glyph-delete',
            text: '删除',
            handler: function () {
                var sm = me.grid.getSelectionModel(),
                    recs = sm.getSelection() || [];

                if (recs.length != 1)
                    return;
                me.deleteSelection(recs);
            }
        });
        this.mySearch = Ext.create('YZSoft.src.form.field.Search', {
            store: me.store,
            width: 160
        });
        var cfg = {
            title: '车辆管理',
            layout: 'fit',
            border: false,
            items: [this.grid],
            tbar: [this.btnNewApp, this.btnEditApp,  this.btnDelete, '->', {
                xtype: 'button',
                text: '清除搜索条件',
                handler: function () {
                    me.clearMySearch();
                }
            }, ' ', me.mySearch]
        };

        Ext.apply(cfg, config);
        me.callParent([cfg]);
        me.updateStatus();
        var sm = this.grid.getSelectionModel();
        if (sm) {
            sm.on('selectionchange', function () {
                me.updateStatus();
            }, me);
        }
    }

    , onActivate: function (times) {
        if (times == 0)
            this.store.load({
                loadMask: {
                    msg: RS.$('All_Loading'),
                    delay: true
                }
            });
        else
            this.store.reload({ loadMask: false });
    },
    addNewApp: function () {
        var me = this;
        YZSoft.BPM.src.ux.FormManager.openFormApplication('ycsoft/oa/oacarmanagelist', '', 'New', Ext.apply({
            sender: this,
            //dlgModel: 'Tab', //Tab,Window,Dialog
            //width: 600,
            //height: 430,
            title: '新增车辆管理',
            listeners: {
                scope: this,
                submit: function (action, data) {
                    me.store.reload({
                        loadMask: {
                            msg: '保存已成功',
                            delay: 'x'
                        },
                        callback: function () {
                            var rec = me.store.getById(data.Key);
                            if (rec)
                                me.grid.getSelectionModel().select(rec);
                        }
                    });
                }
            }
        }, me.dlgCfg));

    },
    addNewProc: function () {
        var me = this;
        YZSoft.BPM.src.ux.FormManager.openPostWindow('车辆管理', Ext.apply({
            sender: this,
            title: '车辆管理',
            //dlgModel: 'Tab', //Tab,Window,Dialog
            //width: 600,
            //height: 430,

            listeners: {
                submit: function (name, result) {
                    me.store.reload({ loadMask: true });
                }
            }
        }, me.dlgCfg));
    },

    editApp: function (rec) {
        var me = this;
        //YZSoft.BPM.src.ux.FormManager.openFormApplication('ycsoft/oa/oacarmanagelist', rec.data.id, 'Edit', Ext.apply({
        YZSoft.BPM.src.ux.FormManager.openFormApplication('ycsoft/oa/oacarmanagelist', rec.data.id, 'Edit', Ext.apply({
            sender: this,
            //title: Ext.String.format('修改-车辆管理 - {0}', rec.data.name),
            title: '修改-车辆管理',
            listeners: {
                submit: function (action, data) {
                    me.store.reload({
                        loadMask: {
                            msg: '保存已成功',
                            delay: 'x'
                        }
                    });
                }
            }
        }, me.dlgCfg));
    },
    editProc: function (rec) {
        var me = this;
        YZSoft.BPM.src.ux.FormManager.openPostWindow('修改车辆管理', Ext.apply({
            sender: this,
            //title: Ext.String.format('修改-车辆管理 - {0}', rec.data.name),
            title: '修改-车辆管理',
            params: rec.data,
            //dlgModel: 'Tab', //Tab,Window,Dialog
            //width: 600,
            //height: 430,
            listeners: {
                submit: function (name, result) {
                    me.store.reload({ loadMask: true });
                }
            }
        }, me.dlgCfg));
    },
    deleteProc: function (recs) {
        var me = this;
        var rec = recs[0];
        YZSoft.BPM.src.ux.FormManager.openPostWindow('删除车辆管理', Ext.apply({
            sender: this,
            //title: Ext.String.format('删除-车辆管理 - {0}', rec.data.name),
            title: '删除-车辆管理',
            params: rec.data,
            //dlgModel: 'Tab', //Tab,Window,Dialog
            //width: 600,
            //height: 430,
            listeners: {
                submit: function (name, result) {
                    me.store.reload({ loadMask: true });
                }
            }
        }, me.dlgCfg));
    },
    read: function (rec) {
        var me = this;
        //YZSoft.BPM.src.ux.FormManager.openFormApplication('ycsoft/oa/oacarmanagelist', rec.data.id, 'Read', Ext.apply({
        YZSoft.BPM.src.ux.FormManager.openFormApplication('ycsoft/oa/oacarmanagelist', rec.data.id, 'Read', Ext.apply({
            sender: this,
            //title: Ext.String.format('车辆管理 - {0}', rec.data.name),
            title: '车辆管理',
        }, me.dlgCfg));
    },
    clearMySearch: function () {
        var me = this;
        var params = me.store.getProxy().getExtraParams();
        params.searchType = '';
        params.keyword = '';

        me.mySearch.setValue('');
        me.mySearch.getTrigger('clear').hide();
        me.mySearch.updateLayout();
        me.mySearch.fireEvent('clearclick');
        me.store.reload({ params: { start: 0} });

    },
    deleteSelection: function (recs) {
        var me = this;
        var ids = [], codes = [];
        if (recs.length == 0)
            return;

        Ext.each(recs, function (rec) {
            //ids.push(rec.getId());
            codes.push(rec.data["id"]);
        });

        Ext.Msg.show({
            title: '删除确认',
            msg: '您确定要删除选中项吗？',
            buttons: Ext.Msg.OKCANCEL,
            defaultFocus: 'cancel',
            icon: Ext.MessageBox.INFO,

            fn: function (btn, text) {
                if (btn != 'ok')
                    return;

                YZSoft.Ajax.request({
                    //url: YZSoft.$url(this, 'Services.ashx'),
                    url: YZSoft.$url("ycsoft/list/oa/services/oacarmanagelistservices.ashx"),
                    method: 'POST',
                    params: {
                        method: 'Delete'
                    },
                    //jsonData: ids,
                    jsonData: codes,
                    waitMsg: { msg: '正在删除...', target: this.grid },
                    success: function (action) {
                        me.store.reload({
                            loadMask: {
                                msg: Ext.String.format('{0}个对象已删除！', recs.length),
                                delay: 'x'
                            }
                        });
                    },
                    failure: function (action) {
                        var mbox = Ext.Msg.show({
                            title: '错误提示',
                            msg: YZSoft.HttpUtility.htmlEncode(action.result.errorMessage, true),
                            buttons: Ext.Msg.OK,
                            icon: Ext.MessageBox.WARNING
                        });

                        me.store.reload({ mbox: mbox });
                    }
                });
            }
        });

    },
    updateStatus: function () {
        this.btnNewApp.setDisabled(!YZSoft.UIHelper.IsOptEnable(this, this.grid, 'New'));
        //this.btnEditApp.setDisabled(!YZSoft.UIHelper.IsOptEnable(null, me.grid, this.perm, 1, 1)
        this.btnEditApp.setDisabled(!YZSoft.UIHelper.IsOptEnable(null, this.grid, 'Edit', 1, 1));
        this.btnEditProc.setDisabled(!YZSoft.UIHelper.IsOptEnable(null, this.grid, 'Edit', 1, 1));
        this.btnDelete.setDisabled(!YZSoft.UIHelper.IsOptEnable(null, this.grid, 'Delete', 1, 1));
        this.btnDeleteProc.setDisabled(!YZSoft.UIHelper.IsOptEnable(null, this.grid,'Delete', 1, 1));
    }

});

